<?php
namespace Content\Controller;
use Think\Controller;

class MyajaxController extends Controller 
{   
  protected $articleObj;
  protected $typeObj;
  protected $status;

  public function _initialize()
  {      
      $this->articleObj = D("Content/Article");
      $this->typeObj = M("Type");
      $this->status = array('egt',0);
  }
  
  public function getArticles()
  {
    $where['status']=$this->status;
    $articles = $this->articleObj->field("id,title")->where($where)->select();
    if ($articles) {
        $arr = array("status"=>'1',"dataValue"=>$articles);
    }else{
        $arr = array("status"=>'0',"msg"=>"获取文章失败");
    }
    echo json_encode($arr);
  }
  public function getTypes()
  {
    $types=$this->typeTree(0,0);
    if ($types) {
        $arr = array("status"=>'1',"dataValue"=>$types);
    }else{
        $arr = array("status"=>'0',"msg"=>"获取分类失败");
    }
    echo json_encode($arr);
  }
  public function typeTree($parentid=0,$level=0,$html='|── ')
  {
      static $tree = array();
      $typeList = $this->getTypeList($parentid);
      if (!empty($typeList)) 
      {
          foreach($typeList as $type)
          {
              $type['sort'] = $level;
              $html=str_repeat($html,$level);
              $type['html'] = $html;
              $tree[] = $type;
              $parentId = $type['id'];
              $this->typeTree($parentId,$level+1);
          }
      }
      return $tree;
  }
  public function getTypeList($parentId)
  {
      $typeList = $this->typeObj->where(array("parentid" => $parentId))->order(array("id" => "ASC"))->select();
      return $typeList;
  }
  
}
?>  